package com.antler.smt.portal.controller;

import com.antler.smt.entity.AttachmentEntity;
import com.antler.smt.entity.MemberEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.annotation.OperateCut;
import com.antler.smt.portal.mapper.AttachmentEntityMapper;
import com.antler.smt.portal.mapper.MemberEntityMapper;
import com.antler.smt.portal.mapper.MemberNameInfoMapper;
import com.antler.smt.portal.mapper.RequirementInfoMapper;
import com.antler.smt.request.portal.GetSupplierPictureReq;
import com.antler.smt.request.portal.OrderTestFileReq;
import com.antler.smt.request.portal.RequirementFileIdReq;
import com.antler.smt.response.base.Result;
import com.antler.smt.response.portal.GetImgResp;
import com.antler.smt.service.portal.*;
import com.antler.smt.utils.LogUtil;
import com.antler.smt.validate.DataValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 显示图片
 *
 * @author zhangyi
 * @date 2018/5/12 11:49
 */
@RestController
@RequestMapping("/fileOp")
@Api(description = "显示图片")
public class FileOpController extends BaseController{
    /**
     * 日志
     */
    private static Logger log = LogUtil.get();

    @Resource
    private SupplierImgService supplierImgService;

    @Resource
    private OrderCommentPictureService orderCommentPictureService;

    @Resource
    private MemberImgService memberImgService;

    @Resource
    private PurchaserService purchaserService;

    @Resource
    private OrderService orderService;

    @Resource
    private RequirementInfoMapper requirementInfoMapper;

    @Resource
    private MemberNameInfoMapper memberNameInfoMapper;

    @Resource
    private MemberEntityMapper memberEntityMapper;

    @Resource
    private AttachmentEntityMapper attachmentEntityMapper;

    /**
     * 获取当前服务商下的的图片信息
     *
     *
     * @return
     */
    @GetMapping("/showSupplierImg")
    @ApiOperation(value = "获取当前服务商下的的图片信息", httpMethod = "GET")
    public Result showSupplierImg(@RequestParam(value = "supplierId") long supplierId){

        DataValidator.isBlank(String.valueOf(supplierId),"优质加工厂id不能为空！");

        Result result = null;
        result = supplierImgService.getSupplierImg(supplierId);
        return result;
    }

    /**
     * 服务商插入类型图片
     *
     * @param supplierPicture 服务商图片实体
     * @return
     */
    @PostMapping("/insertSupplierImg")
    @ApiOperation(value = "服务商插入类型图片", httpMethod = "POST")
    @OperateCut(operationContent = "服务商插入类型图片")
    public Result<ResultCodeEnum> supplierImg(@RequestBody GetSupplierPictureReq supplierPicture){

        DataValidator.isNull(supplierPicture,"请求服务商实体不能为空！");
        DataValidator.isBlank(String.valueOf(supplierPicture.getAttachmentId()),"图片id数组不能为空！");
        DataValidator.isBlank(String.valueOf(supplierPicture.getType()),"类型不能为空！");

        Result result = null;
        result = supplierImgService.insertSupplierImg(supplierPicture,currentMember.getId());
        return result;
    }

    /**
     * 关联订单评价图片
     *
     * @param orderCommentId 订单评价id
     * @param attachmentIds 上传的文件id
     * @return
     */
    @PostMapping("/connectOrderCommentImg")
    @ApiOperation(value = "关联订单评价图片（暂时不用！）", httpMethod = "POST")
    public Result<List<String>> connectOrderCommentImg(@RequestParam("orderCommentId") Long orderCommentId,
                                                       @RequestParam("attachmentIds") Long attachmentIds){

        DataValidator.isBlank(String.valueOf(orderCommentId),"订单评价id不能为空！");
        DataValidator.isBlank(String.valueOf(attachmentIds),"上传的文件id数组不能为空！");
        Result result = null;
        //...
        return result;
    }

    /**
     * 获取订单评价图片
     *
     * @param orderCommentId 订单id
     * @return
     */
    @GetMapping("/showOrderCommentImg")
    @ApiOperation(value = "获取订单评价图片", httpMethod = "GET")
    public Result showOrderCommentImg(@RequestParam("orderCommentId") Long orderCommentId){

        DataValidator.isBlank(String.valueOf(orderCommentId),"订单评价id不能为空！");

        Result result = null;
        result = orderCommentPictureService.showOrderCommentImg(orderCommentId);
        return result;
    }



    /**
     * 获取该会员下的头像
     *
     * @return
     */
    @GetMapping("/getMemberImg")
    @ApiOperation(value = "获取该会员下的头像", httpMethod = "GET")
    public Result<GetImgResp> getMemberImg(){

        Result result = null;
        result = memberImgService.getImgInfo(currentMember.getId());
        return result;
    }



    /**
     * 获取该采购商下的认证营业执照图片
     *
     * @return
     */
    @GetMapping("/getPurchaserImg")
    @ApiOperation(value = "获取该采购商下的认证营业执照图片", httpMethod = "GET")
    public Result<GetImgResp> getPurchaserImg(){

        Result result = null;
        result = purchaserService.getCertificationImg(currentMember.getId());
        return result;
    }


    /**
     * 在订单下上传文件附件
     *
     * @return
     */
    @PostMapping("/uploadOrderTestFile")
    @ApiOperation(value = "在订单下上传文件附件", httpMethod = "POST")
    @OperateCut(operationContent = "上传文件")
    public Result uploadOrderTestFile(@RequestBody OrderTestFileReq orderTestFileReq){

        DataValidator.isNull(orderTestFileReq,"上传文件实体不为空！");
        DataValidator.isBlank(String.valueOf(orderTestFileReq.getAttachementId()),"文件附件id");
        DataValidator.isBlank(String.valueOf(orderTestFileReq.getOrderId()),"订单id不能为空！");

        Result result = null;
        result = orderService.updateUploadTestFile(orderTestFileReq);
        return result;
    }



    /**
     * 前台下载文件（发布需求）
     *
     * @return
     */
    @GetMapping("/downloadFile")
    @ApiOperation(value = "前台下载文件（发布需求） 一次获取一张", httpMethod = "GET")
    @OperateCut(operationContent = "下载文件")
    public Result downloadFile(@RequestParam(value = "requirementId") long requirementId,
                               @RequestParam(value = "attachmentId") long attachmentId){

        DataValidator.isBlank(String.valueOf(requirementId),"发布需求id不能为空！");
        DataValidator.isBlank(String.valueOf(attachmentId),"文件id不能为空！");

        Result result = null;
        RequirementFileIdReq re = new RequirementFileIdReq();
        re.setAttachmentId(attachmentId);
        re.setRequirementId(requirementId);
        String fileName = requirementInfoMapper.getFileName(re);
        result = new Result(fileName);
        return result;
    }


    /**
     * 用户生成默认头像
     *
     * @return
     */
    @PostMapping("/createMemberImg")
    @ApiOperation(value = "用户生成默认头像", httpMethod = "POST")
    public Result createMemberImg(){
        Result result = null;
            MemberEntity memberEntity1 = memberEntityMapper.selectByPrimaryKey(currentMember.getId());
            if(0 == memberEntity1.getHeadImageAttachmentId()) {
                MemberEntity memberEntity = new MemberEntity();
                memberEntity.setId(currentMember.getId());
                //设置默认的头像id
                memberEntity.setHeadImageAttachmentId(new Long(9));
                int i = memberNameInfoMapper.updateHeadImage(memberEntity);
                result = (0 == i ? new Result(ResultCodeEnum.APPEND_DATA_ERROR) : new Result(ResultCodeEnum.OPERATION_SUCCESS));
            }else{
                result =  new Result(ResultCodeEnum.APPEND_DATA_ERROR,"该用户已有头像");
            }
        return result;
    }


    /**
     * 根据图片id获取图片信息
     *
     * @return
     */
    @GetMapping("/getFileById")
    @ApiOperation(value = "根据图片id获取图片信息", httpMethod = "GET")
    public Result getFileById(@RequestParam(value = "id")Long id){

        DataValidator.isBlank(String.valueOf(id),"图片id不能为空！");

        Result result = null;
            AttachmentEntity attachmentEntity =
                    attachmentEntityMapper.selectByPrimaryKey(id);
            if(attachmentEntity != null) {
                result = new Result(attachmentEntity.getFileName());
            }
        return result;
    }
}
